package com.gqz.equip.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.gqz.common.core.domain.RequestForm;
import com.gqz.common.core.utils.query.QueryMapperUtil;
import com.gqz.equip.entity.TBizcEneryEquipEntity;
import com.gqz.equip.mapper.TBizcEneryEquipMapper;
import com.gqz.equip.mapstruct.TBizcEneryEquipMapstruct;
import com.gqz.equip.service.ITBizcEneryEquipService;
import com.gqz.equip.service.ITEnInterfaceCodeService;
import com.gqz.equip.vo.TBizcEneryEquipVo;
import com.gqz.equip.vo.TEnInterfaceCodeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 新能源设备信息 业务逻辑实现层ServiceImpl
 *
 * @Version 1.0
 * @Author input you name
 */
@Service
@Slf4j
public class TBizcEneryEquipServiceImpl extends ServiceImpl<TBizcEneryEquipMapper, TBizcEneryEquipEntity> implements ITBizcEneryEquipService {

    @Autowired
    private TBizcEneryEquipMapstruct mapstruct;
    @Autowired
    private ITEnInterfaceCodeService codeService;

    @Override
    public List<TBizcEneryEquipVo> getById(String id) {
        MPJLambdaWrapper<TBizcEneryEquipEntity> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(TBizcEneryEquipEntity::getSbbm, id);
        queryWrapper.selectAll(TBizcEneryEquipEntity.class);
        List<TBizcEneryEquipEntity> entities = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(entities)) {
            return mapstruct.toVos(entities);
        }
        return new ArrayList<>();
    }

    @Override
    public Page<TBizcEneryEquipVo> page(RequestForm<TBizcEneryEquipVo> requestForm) {
        TBizcEneryEquipEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<TBizcEneryEquipEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        queryWrapper.selectAll(TBizcEneryEquipEntity.class);
        queryWrapper.eq(TBizcEneryEquipEntity::getSfsc, 0);
        queryWrapper.orderByAsc(TBizcEneryEquipEntity::getCcTime);
        Page<TBizcEneryEquipEntity> page = Page.of(requestForm.getPageNo(), requestForm.getPageSize());
        //page = baseMapper.selectPage(page, queryWrapper);

        List<TEnInterfaceCodeVo> tEnInterfaceCodeList = codeService.queryDictData("EN_EQUIP_SBLX");
        //IPage<TBizcEneryEquipEntity> iPage = baseMapper.selectJoinPage(page, TBizcEneryEquipEntity.class, queryWrapper);
        Page<TBizcEneryEquipEntity> iPage = baseMapper.selectPage(page, queryWrapper);
        Page<TBizcEneryEquipVo> TBizcEneryEquipVoPage = mapstruct.toVos(iPage);

        List<TBizcEneryEquipVo> records = TBizcEneryEquipVoPage.getRecords().stream().map(item -> {
            if (item != null) {
                if (item.getSblx() != null) {
                    List<TEnInterfaceCodeVo> collect = tEnInterfaceCodeList.stream().filter(codeVo -> codeVo.getCode().equals(item.getSblx().toString())).collect(Collectors.toList());
                    if (!collect.isEmpty()) {
                        item.setSblxmc(collect.get(0).getValue());
                    }
                }
                if ("0".equals(item.getDjhszt())) {
                    item.setDjhszt("未收回");
                } else {
                    item.setDjhszt("已收回");
                }
                if ("10".equals(item.getYxzt())) {
                    item.setYxzt("未投运");
                } else if ("20".equals(item.getYxzt())) {
                    item.setYxzt("在运");
                } else if ("30".equals(item.getYxzt())) {
                    item.setYxzt("退役");
                } else {
                    item.setYxzt("报废");
                }
            }

            return item;
        }).collect(Collectors.toList());

        TBizcEneryEquipVoPage.setRecords(records);
        // TBizcEneryEquipVoPage.setTotal(iPage.getTotal());
        return TBizcEneryEquipVoPage;
    }

    @Override
    public Long count(RequestForm<TBizcEneryEquipVo> requestForm) {
        TBizcEneryEquipEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<TBizcEneryEquipEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        queryWrapper.select(TBizcEneryEquipEntity::getSbbm);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public int insert(List<TBizcEneryEquipVo> vos) {
        List<TBizcEneryEquipEntity> entitys = mapstruct.toEntitys(vos);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            TBizcEneryEquipEntity entity = entitys.get(i);
            result += baseMapper.insert(entity);
        }
        return result;
    }

    @Override
    public int update(List<TBizcEneryEquipVo> vos) {
        List<TBizcEneryEquipEntity> entitys = mapstruct.toEntitys(vos);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            TBizcEneryEquipEntity entity = entitys.get(i);
            result += baseMapper.updateById(entity);
        }
        return result;
    }

    @Override
    public List<TBizcEneryEquipVo> getList(RequestForm<TBizcEneryEquipVo> requestForm) {
        TBizcEneryEquipEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<TBizcEneryEquipEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        queryWrapper.selectAll(TBizcEneryEquipEntity.class);
        queryWrapper.eq(TBizcEneryEquipEntity::getSfsc, 0);
        queryWrapper.orderByAsc(TBizcEneryEquipEntity::getCcTime);
        List<TBizcEneryEquipEntity> codeEntityList = baseMapper.selectList(queryWrapper);

        List<TEnInterfaceCodeVo> tEnInterfaceCodeList = codeService.queryDictData("EN_EQUIP_SBLX");
        List<TBizcEneryEquipVo> TBizcEneryEquipVoList = mapstruct.toVos(codeEntityList);
        if (TBizcEneryEquipVoList.size() > 0) {
            for (int i = 0; i < TBizcEneryEquipVoList.size(); i++) {
                if (TBizcEneryEquipVoList.get(i).getSblx() != null) {
                    TBizcEneryEquipVo eneryEquipVo = TBizcEneryEquipVoList.get(i);
                    List<TEnInterfaceCodeVo> collect = tEnInterfaceCodeList.stream().filter(codeVo -> codeVo.getCode().equals(eneryEquipVo.getSblx().toString())).collect(Collectors.toList());
                    if (!collect.isEmpty()) {
                        TBizcEneryEquipVoList.get(i).setSblxmc(collect.get(0).getValue());
                    }
                }
                if ("0".equals(TBizcEneryEquipVoList.get(i).getDjhszt())) {
                    TBizcEneryEquipVoList.get(i).setDjhszt("未收回");
                } else {
                    TBizcEneryEquipVoList.get(i).setDjhszt("已收回");
                }
                if ("10".equals(TBizcEneryEquipVoList.get(i).getYxzt())) {
                    TBizcEneryEquipVoList.get(i).setYxzt("未投运");
                } else if ("20".equals(TBizcEneryEquipVoList.get(i).getYxzt())) {
                    TBizcEneryEquipVoList.get(i).setYxzt("在运");
                } else if ("30".equals(TBizcEneryEquipVoList.get(i).getYxzt())) {
                    TBizcEneryEquipVoList.get(i).setYxzt("退役");
                } else {
                    TBizcEneryEquipVoList.get(i).setYxzt("报废");
                }
            }
        }
        return TBizcEneryEquipVoList;
    }


}
